package com.shanwen.shopping.activity;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.shanwen.shopping.R;
import com.shanwen.shopping.utils.EventUtil;
import com.shanwen.shopping.utils.GoodsDaoUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 文件描述：Activity父类
 * 创 建 人：刘宾
 * 创建时间：2016/3/28 11:17
 */
public abstract class AppBaseActivity extends BaseActivity {
    @BindView(R.id.common_titlebar_title_tv)TextView tvTitle;
    @BindView(R.id.common_titlebar_shopping_cart_tv)TextView tvShoppingCart;
    @BindView(R.id.common_titlebar_back_iv)LinearLayout ivBack;
    @BindView(R.id.common_titlebar_right_iv)ImageView ivRight;
    @BindView(R.id.common_titlebar_bottom_line)View vLine;
    private Unbinder unbinder;
    //    @BindView(R.id.common_titlebar_layout)View vTitlebarLayout;

    public boolean configEventBus() {
        return true;
    }

    @Override public void initView() {
        unbinder = ButterKnife.bind(this);
        if(null!=ivBack){
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    onBackClick();
                }
            });
        }
//        if(null != tvShoppingCart){
//            tvShoppingCart.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    onShoppingCartClick();
//                }
//            });
//        }
    }

    public void onBackClick(){
        finish();
    }
//    public void onShoppingCartClick(){
//        startActivity(activity,ShoppingCartActivity.class);
//    }

    @Override public void register() {
        if (configEventBus()) {
            EventUtil.registerEventBus(this);
        }
    }

    @Override public void unRegister() {
        unbinder.unbind();
        if (configEventBus()) {
            EventUtil.unRegisterEventBus(this);
        }
    }

    /** 是否显示标题栏底部分割线 */
    public void setBottomLineEnabled(boolean enabled){
        if(null != vLine){
            vLine.setVisibility(enabled ? View.VISIBLE : View.GONE);
        }
    }
    /** 是否显示购物车入口 */
    public void setShoppingCartEnabled(boolean enabled){
        if(null != tvShoppingCart){
            tvShoppingCart.setVisibility(enabled ? View.VISIBLE : View.GONE);
        }
    }
    /** 是否显示返回按钮 */
    public void setBackEnabled(boolean enabled){
        if(null != ivBack){
            ivBack.setVisibility(enabled ? View.VISIBLE : View.GONE);
        }
    }
    /** 设置返回按钮图标 */
    @SuppressWarnings("ResourceType")
//    public void setBackIcon(@IdRes int resId){
//        if(null != ivBack){
//            ivBack.setImageResource(resId);
//        }
//    }
    /** 是否显示右侧按钮 */
    public void setRightEnabled(boolean enabled){
        if(null != ivRight){
            ivRight.setVisibility(enabled ? View.VISIBLE : View.GONE);
        }
    }
    /** 设置右侧按钮图标 */
    @SuppressWarnings("ResourceType")
    public void setRightIcon(@IdRes int resId){
        if(null != ivRight){
            ivRight.setImageResource(resId);
        }
    }
    /** 设置标题 */
    public void setTitle(@NonNull String title){
        tvTitle.setText(title);
    }
    /** 设置标题 */
    @SuppressWarnings("ResourceType")
    public void setTitle(@IdRes int resId){
        tvTitle.setText(resId);
    }


    public long mLastActionTime;
    public Timer mTimer;
    public MyTimerTask mTimerTask;

    public static long TIMER_TIME = Long.MAX_VALUE;

    @Override
    protected void onPause() {
        cancelTimer();
        TIMER_TIME = Long.MAX_VALUE;
        super.onPause();
    }

    @Override
    protected void onResume() {
        startTimer();
        super.onResume();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mLastActionTime = System.currentTimeMillis();
        return super.dispatchTouchEvent(ev);
    }

    protected void startTimer() {
        mTimer = new Timer();
        mTimerTask = new MyTimerTask();
        mLastActionTime = System.currentTimeMillis();
        mTimer.schedule(mTimerTask, 0, 1000);
    }


    private class MyTimerTask extends TimerTask {

        @Override
        public void run() {
            if (System.currentTimeMillis() - mLastActionTime > TIMER_TIME) {
                cancelTimer();
                new GoodsDaoUtil(AppBaseActivity.this).deleteAll();
                Intent intent = new Intent(AppBaseActivity.this, NewMainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        }
    }


    private void cancelTimer(){
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }
}
